﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EShop_ManagementBrandDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) return;
        GetCityInfo();
        GetDistrict();
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            lblBrandID.Text = Request.QueryString["id"];
            ShowBrandInfo();
        }
    }
    public void GetCityInfo()
    {
        try
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var query = (from o in dc.Cities
                             where o.active==1
                             select o).ToList();
                ddlCity.DataSource = query;
                ddlCity.DataTextField = "city_name";
                ddlCity.DataValueField = "id";
                ddlCity.DataBind();
            }
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    public void GetDistrict()
    {
        try
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var query = (from o in dc.Districts
                             where o.active == 1 && o.id_city==int.Parse(ddlCity.SelectedValue)
                             select o).ToList();
                ddlDistrict.DataSource = query;
                ddlDistrict.DataTextField = "district_name";
                ddlDistrict.DataValueField = "id";
                ddlDistrict.DataBind();
            }
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }
    void ShowBrandInfo()
    {
        using (Up1VNDataContext dc = new Up1VNDataContext())
        {
            HttpCookie _user = Request.Cookies["userinfo"];
            var query = (from o in dc.brands
                         where o.brand_user == _user["username"].ToString() && o.active == 1 && o.id==int.Parse( lblBrandID.Text)
                         select o).ToList();
            if (query.Count > 0)
            {
                txtBrandName.Text = query[0].name;
                txtCategoryJob.Text = query[0].category_job;
                txtCustomerBrand.Text = query[0].customer_brand;
                txtMobile.Text = query[0].mobile;
                txtPhone.Text = query[0].phone;
                txtFax.Text = query[0].fax;
                txtEmail.Text = query[0].email;
                txtWebsite.Text = query[0].url;
                txtTaxCode.Text = query[0].tax_code;
                txtDateCreate.Text = query[0].date_created.Value.ToString("dd/MM/yyyy");
                ckVip.Checked = query[0].isVip==1 ? true : false;
                ckCheck.Checked = query[0].isCheck == 1 ? true : false;
                txtAddress.Text = query[0].address;
                ddlCity.SelectedValue = query[0].id_city.ToString();
                ddlDistrict.SelectedValue = query[0].id_district.ToString();
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(lblBrandID.Text))
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                if (ckVip.Checked)
                {
                    var query = (from es in dc.EStores
                                 join esp in dc.EStorePoints on es.EStore_Id equals esp.EStore_Id
                                 where es.EStore_CategoryId == int.Parse(lblBrandID.Text) && esp.EStorePoint_Point < es.EStore_TotalPoint
                                 select es).ToList(); 
                    if (query.Count <= 0)
                    {
                        lblMessage.Visible = true;
                        return;
                    }

                }
                    brand _brand = dc.brands.Single(s => s.id == int.Parse(lblBrandID.Text));
                    _brand.name = txtBrandName.Text;
                    _brand.category_job = txtCategoryJob.Text;
                    _brand.customer_brand = txtCustomerBrand.Text;
                    _brand.mobile = txtMobile.Text;
                    _brand.phone = txtPhone.Text;
                    _brand.fax = txtFax.Text;
                    _brand.email = txtEmail.Text;
                    _brand.url = txtWebsite.Text;
                    _brand.tax_code = txtTaxCode.Text;
                    _brand.date_created = Convert.ToDateTime(txtDateCreate.Text);
                    _brand.isVip = ckVip.Checked ? 1 : 0;
                    _brand.isCheck = ckCheck.Checked ? 1 : 0;
                    _brand.address = txtAddress.Text;
                    _brand.id_city = int.Parse(ddlCity.SelectedValue);
                    _brand.id_district = int.Parse(ddlCity.SelectedValue);
                    dc.brands.Attach(_brand);
                    dc.SubmitChanges();
            }
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(lblBrandID.Text))
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var a = (from o in dc.brands where o.id == int.Parse(lblBrandID.Text) select o).SingleOrDefault();
                if (a != null)
                    dc.DeleteDatabase();
                dc.SubmitChanges();
            }
        }
        
    }
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (Up1VNDataContext dc = new Up1VNDataContext())
        {
            var query = (from o in dc.Districts
                         where o.id_city == int.Parse(ddlCity.SelectedValue) && o.active == 1
                         select o).ToList();
            ddlDistrict.Items.Clear();
            if (query.Count > 0)
            {
                ddlDistrict.DataSource = query;
                ddlDistrict.DataTextField = "district_name";
                ddlDistrict.DataValueField = "id";
                ddlDistrict.DataBind();
            }
        }
    }
    
}